﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Sockets;
using System.Text;
using Skylabs.Net;
using Skylabs.Net.Sockets;

namespace Skylabs.Breeze
{
    public class Client : SkySocket,IEquatable<Client>
    {
        public int Id { get; private set; }
        public String FileName { get; private set; }
        public FileReceiver Parent;
        public Client(TcpClient client, int id):base(client)
        {
            Id = id;

        }
        public bool Equals(Client other)
        {
            return other.Id == Id;
        }

        public override void OnMessageReceived(Net.SocketMessage sm)
        {
            string m = sm.Header.ToLower();
            switch (m)
            {
                case "hello":
                    {
                        Server.ClientHello(this,(string)sm["filename"]);
                        WriteMessage(new SocketMessage("start"));
                        break;
                    }
                case "filestart":
                    {
                        Program.ServerTrace.TraceInformation("File transfer started {0}", (string)sm["filename"]);
                        //Server.FileReceivers.Add(new FileReceiver((string)sm["filename"], (long)sm["size"], (string)sm["hash"]));
                        Parent.Start((long)sm["size"], (string)sm["hash"]);
                        break;
                    }
                case "filepart":
                    {
                        //Server.FileReceivers.Where(t => t.FileName == (string)sm["filename"]).First().AddPart((long)sm["position"], (int)sm["size"], (byte[])sm["data"]);
                        Parent.AddPart((long)sm["position"], (int)sm["size"], (byte[])sm["data"]);
                        break;
                    }
                case "partdone":
                    {
                        //Server.ClientDone(this, (string)sm["filename"]);
                        Parent.ClientDone(this);
                        break;
                    }
                case "filedone":
                    {
                        //String fn = (string) sm["filename"];
                        //Server.FileReceivers.Where(t => t.FileName == fn).First().Done();
                        break;
                    }
            }
        }

        public override void OnDisconnect(Net.DisconnectReason reason)
        {
            //Server.ClientDone(this);
        }
    }
}
